package com.ken;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CServlet", urlPatterns = {"/Cpage"})
public class CServlet extends javax.servlet.http.HttpServlet {


    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("utf-8");
//        String username = request.getParameter("username");

//        System.out.println("接受到的POST请求参数 Cpage username："+username);
        response.setContentType("text/html;charset = utf-8");
        String username = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")) {
                    username = cookie.getValue();
                }
            }
        }


        StringBuffer stf = new StringBuffer();

        PrintWriter out = response.getWriter();
        stf.append("<!DOCTYPE html>");
        stf.append("<html lang=\"en\">");
        stf.append("<head>");
        stf.append("<meta charset=\"UTF-8\">");
        stf.append(" <title>Bpage</title>");
        stf.append("</head>");
        stf.append("<body>");
        stf.append("<h3 >Web状态保持,数据传递</h3>");
        stf.append(" 用户名：").append(username).append("<br>");
        stf.append("  <a href=\"deleteservlet\">跳转到删除cookie页面</a>");
        stf.append(" </body>");
        stf.append(" </html>");

        out.print(stf.toString());
        out.close();
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doPost(request, response);
    }
}
